package com.blb.zycommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.SysRole;
import com.blb.zycommunity.entity.ZyOwner;
import com.blb.zycommunity.mapper.ZyOwnerMapper;
import com.blb.zycommunity.service.IZyOwnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 业主  服务实现类
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@Service
public class ZyOwnerServiceImpl extends ServiceImpl<ZyOwnerMapper, ZyOwner> implements IZyOwnerService {

    @Resource
    private ZyOwnerMapper ownerMapper;
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public IPage<ZyOwner> findOwnerPage(Long current, Long size,String communityId, String ownerNickname, String ownerRealName, String ownerIdCard, String ownerPhoneNumber) {
        return ownerMapper.selectOwnerPage(new Page<>(current,size),communityId,ownerNickname,ownerRealName,ownerIdCard,ownerPhoneNumber);
    }

    @Override
    public List<ZyOwner> findAuditRecords(String ownerRealName) {
        return ownerMapper.selectAuditRecords(ownerRealName);
    }

    @Override
    public void register(ZyOwner zyOwner) throws Exception {
        ZyOwner zyOwner1 = ownerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_nickname", zyOwner.getOwnerNickname()));
        if (zyOwner1 != null) {
            throw new Exception("用户已存在");
        }
        zyOwner.setOwnerPassword(bCryptPasswordEncoder.encode(zyOwner.getOwnerPassword()));
        ownerMapper.insert(zyOwner);
    }

    @Override
    public List<GrantedAuthority> getAuthoritiesByNickname(String ownerNickname) {
        //给用户进行授权
        StringBuilder stringBuilder = new StringBuilder();
        //将字符串转换为权限集合
        List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(stringBuilder.toString());
        return authorities;
    }

    @Override
    public ZyOwner findByNickname(String ownerNickname) {
        ZyOwner owner = ownerMapper.selectByOwnerNickname(ownerNickname);
        return owner;
    }

    @Override
    public List<ZyOwner> findRoomByNickname(String ownerNickname) {
        return ownerMapper.selectRoomByOwnerNickname(ownerNickname);
    }

    @Override
    public List<ZyOwner> findCommunityIdByNickname(String ownerNickname) {
        return ownerMapper.selectCommunityIdByNickname(ownerNickname);
    }

    @Override
    public void updateOwnerPortrait(String ownerPortrait, String ownerId) {
        ownerMapper.updateOwnerPortrait(ownerPortrait,ownerId);
    }

}
